package com.blate.dogxiao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blate.dogxiao.R;
import com.blate.dogxiao.Repo;
import com.blate.dogxiao.adapter.ConversationMessageAdapter;
import com.blate.dogxiao.adapter.SimpleDataRecyclerViewAdapter;
import com.blate.dogxiao.bean.ConversationBean;
import com.blate.dogxiao.bean.UserBean;
import com.blate.dogxiao.databinding.ActivityConversationMessageBinding;
import com.blate.dogxiao.databinding.ItemConversationMessageBinding;
import com.blate.dogxiao.decoration.VerticalListSpaceDecoration;
import com.blate.dogxiao.function.AutoIoScheduler;
import com.blate.dogxiao.tools.ScreenSizeTools;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * @author Blate
 * Created in 2021/4/21
 */
public class ConversationMessageActivity
        extends AppCompatActivity {

    public static final String KEY_CONVERSATION = "key_conversation";

    private ActivityConversationMessageBinding mBinding;

    private ConversationBean mConversationBean;
    private UserBean mSelf;

    private ConversationMessageAdapter mMessageAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_conversation_message);
        readExtra(getIntent());
        initialize();
    }

    private void readExtra(Intent intent) {
        mConversationBean = intent.getParcelableExtra(KEY_CONVERSATION);
    }

    private void initialize() {
        mBinding.topBar.setNavigationOnClickListener(v -> finish());
        mBinding.topBar.setTitle(mConversationBean.nickname);
        mBinding.rvMessage.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mBinding.rvMessage.addItemDecoration(new VerticalListSpaceDecoration(
                0,
                0,
                ScreenSizeTools.dpToPx(this, 16f),
                ScreenSizeTools.dpToPx(this, 16f),
                ScreenSizeTools.dpToPx(this, 16f)));
        mMessageAdapter = new ConversationMessageAdapter();
        mMessageAdapter.setItemChildClickListener(
                new SimpleDataRecyclerViewAdapter.MonitorViewCollector<ItemConversationMessageBinding>() {
                    @Override
                    public void collectorMonitorView(SimpleDataRecyclerViewAdapter.ItemViewMonitor itemViewMonitor, ItemConversationMessageBinding binding) {
                        itemViewMonitor.monitorView(binding.civAvatar);
                    }
                }, new SimpleDataRecyclerViewAdapter.OnItemChildClickListener<ItemConversationMessageBinding, ConversationBean.Msg>() {
                    @Override
                    public void onItemChildClick(ItemConversationMessageBinding binding, View view, ConversationBean.Msg data) {
                        Intent intent = new Intent(ConversationMessageActivity.this, UserProfileActivity.class);
                        if (data.owner == ConversationBean.Msg.SELF) {
                            intent.putExtra(UserProfileActivity.KEY_USER_BEAN, mSelf);
                        } else if (data.owner == ConversationBean.Msg.OTHER) {
                            UserBean userBean = new UserBean();
                            userBean.avatar = mConversationBean.avatar;
                            userBean.nickname = mConversationBean.nickname;
                            userBean.gender = mConversationBean.gender;
                            userBean.phoneNumber = mConversationBean.phoneNumber;
                            intent.putExtra(UserProfileActivity.KEY_USER_BEAN, userBean);
                        }
                        startActivity(intent);
                    }
                }
        );
        mBinding.rvMessage.setAdapter(mMessageAdapter);
        loadData();
    }

    private void loadData() {
        Observable
                .fromCallable(new Callable<Pair<UserBean, ConversationBean>>() {
                    @Override
                    public Pair<UserBean, ConversationBean> call() throws Exception {
                        return new Pair<>(Repo.getSelfBean(ConversationMessageActivity.this), mConversationBean);
                    }
                })
                .compose(new AutoIoScheduler<>())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new Consumer<Pair<UserBean, ConversationBean>>() {
                    @Override
                    public void accept(Pair<UserBean, ConversationBean> selfBeanConversationBeanPair) throws Exception {
                        mSelf = selfBeanConversationBeanPair.first;
                        mMessageAdapter.setSelfAvatarUrl(mSelf.avatar);
                        mMessageAdapter.setSelfNickName(mSelf.nickname);
                        mMessageAdapter.setOtherAvatarUrl(selfBeanConversationBeanPair.second.avatar);
                        mMessageAdapter.setOtherNickname(selfBeanConversationBeanPair.second.nickname);
                        mMessageAdapter.putData(true, selfBeanConversationBeanPair.second.msgs);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.i("Blate", "accept: " + throwable);
                    }
                });
    }

}
